package com.supersit.greensproject.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.adapter.MenuAdapter;
import com.supersit.greensproject.application.MyApplication;
import com.supersit.greensproject.fragment.PirceGatherEnteringFragment;
import com.supersit.greensproject.fragment.PriceGatherSelectedDataFragment;
import com.supersit.greensproject.imagecycleview.ImageCycleView;
import com.supersit.greensproject.resources.Authority;
import com.supersit.greensproject.resources.Resources;
import com.supersit.greensproject.service.NetworkStateService;
import com.supersit.greensproject.utils.AppManager;
import com.supersit.greensproject.utils.T;
import com.supersit.greensproject.view.MyTitleBar;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity {
    public String TAG = MainActivity.class.getSimpleName();
    private long exitTime = 0;
    private Context context;
    private MyTitleBar myTitleBar;
    @ViewInject(R.id.ad_imagecycleview)
    private ImageCycleView imageCycleView;
    @ViewInject(R.id.gv_menu)
    private GridView gvMenu;
    private MenuAdapter adapter;
    private ArrayList<Map<Integer, Map<String, Object>>> menuData;
    private ArrayList<String> mImageUrl = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        setTitleBar();
        setImageCycleView();
        Intent intent = new Intent(context, NetworkStateService.class);
        startService(intent);
        setAdapterData();
        bindListener();
    }

    private void setTitleBar() {
        myTitleBar = new MyTitleBar(context, findViewById(R.id.main_title));
        myTitleBar.setTitleText(context.getResources().getString(R.string.main_title_name));
        myTitleBar.setChildVisibility(false, myTitleBar.getmImgRight());
        myTitleBar.setChildVisibility(false, myTitleBar.getmImgLeft());
    }

    private ImageCycleView.ImageCycleViewListener mAdCycleViewListener = new ImageCycleView.ImageCycleViewListener() {

        @Override
        public void onImageClick(int position, View imageView) {
//            Intent intent = new Intent(context, WebActivity.class);
//            intent.putExtra("url", urls.get(position));
//            startActivity(intent);
        }

        @Override
        public void displayImage(String imageURL, ImageView imageView) {
//            ImageOptions imageOptions = new ImageOptions.Builder()
//                    .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句，默认是true
//                    .setImageScaleType(ImageView.ScaleType.FIT_XY)
//                    .setFailureDrawableId(R.drawable.ic_error)
//                    .setLoadingDrawableId(R.drawable.ic_error)
//                    .build();
//            x.image().bind(imageView, imageURL, imageOptions);
            if ("icv1".equals(imageURL)) {
                imageView.setImageResource(R.drawable.icv1);
            } else if ("icv2".equals(imageURL)) {
                imageView.setImageResource(R.drawable.icv2);
            }
        }
    };

    /**
     * 设置图片轮播数据
     */
    private void setImageCycleView() {
//        mImageUrl = new ArrayList<String>();
//        urls = new ArrayList<String>();
//        mImageUrl.add(imageUrl1);
//        urls.add(url1);
//        mImageUrl.add(imageUrl2);
//        urls.add(url2);
//        mImageUrl.add(imageUrl3);
//        urls.add(url3);
        mImageUrl = new ArrayList<String>();
        mImageUrl.add("icv1");
        mImageUrl.add("icv2");
        imageCycleView = (ImageCycleView) findViewById(R.id.ad_imagecycleview);
        imageCycleView.setImageResources(mImageUrl, mAdCycleViewListener);

    }

    /**
     * 给九宫格菜单设置数据
     */
    private void setAdapterData() {
        int[] maketAuthority = Authority.getMaketAuthority(context);
        menuData = new ArrayList<Map<Integer, Map<String, Object>>>();
        for (int i = 0; i < Resources.mainMenu.length; i++) {
            if (0 == maketAuthority.length && i == 2) {
                continue;
            } else {
                Map<Integer, Map<String, Object>> item = new HashMap<>();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("itemImage", Resources.mainIcon[i]);
                map.put("itemText", Resources.mainMenu[i]);
                item.put(i, map);
                menuData.add(item);
            }
        }
        adapter = new MenuAdapter(context, menuData);
        gvMenu.setAdapter(adapter);
        gvMenu.setOnItemClickListener(onItemClickListener);
    }

    /**
     * 设置事件监听
     */
    private void bindListener() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                T.showShort(context, "再按一次退出");
                exitTime = System.currentTimeMillis();
            } else {
                Intent intent = new Intent(context, NetworkStateService.class);
                stopService(intent);
                AppManager.getAppManager().AppExit(context);
            }

            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int num = 0;
            Map<Integer, Map<String, Object>> item = menuData.get(position);
            for (int key : item.keySet()) {
                num = key;
            }
            switch (num) {
                case 0:
                    startActivity(new Intent(context, SubjectDataActivity.class));
                    break;
                case 1:
                    startActivity(new Intent(context, UploadDataActivity.class));
                    break;
                case 2:
                    MyApplication.list.clear();
                    PriceGatherSelectedDataFragment priceGatherSelectedDataFragment = new PriceGatherSelectedDataFragment();
                    PirceGatherEnteringFragment pirceGatherEnteringFragment = new PirceGatherEnteringFragment();
                    MyApplication.list.add(priceGatherSelectedDataFragment);
                    MyApplication.list.add(pirceGatherEnteringFragment);
                    startActivity(new Intent(context, PriceGatherActivity.class));
                    break;
                case 3:
                    startActivity(new Intent(context, MassDetectionActivity.class));
                    break;
                case 4:
                    startActivity(new Intent(context, SynchronizationMenuActivity.class));
                    break;
                case 5:
                    startActivity(new Intent(context, SettingActivity.class));
                    break;
                default:
                    break;
            }
        }
    };
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };
}
